// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.validator;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.Validator;
import org.apache.tapestry5.ioc.MessageFormatter;
import org.apache.tapestry5.services.FormSupport;

/**
 * 对email的校验
 * @author jcai
 * @version $Revision: 225 $
 * @since 0.0.2
 */
public class Email  implements Validator<Void, String> {

	public Class<Void> getConstraintType() {
		return null;
	}

	public String getMessageKey() {
		return "email";
	}

	public Class<String> getValueType() {
		return String.class;
	}

	public boolean invokeIfBlank() {
		return false;
	}

	public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer, RenderSupport pageRenderSupport) {
	
		
	}
    private String buildMessage(MessageFormatter formatter, Field field)
    {
        return formatter.format(field.getLabel());
    }

	public void validate(Field field, Void constraintValue, MessageFormatter formatter, String value) throws ValidationException {
		if(!value.matches("\\w{1,}[@][\\w\\-]{1,}([.]([\\w\\-]{1,})){1,3}$")){
			throw new ValidationException(buildMessage(formatter, field));
		}
	}

	public boolean isRequired() {
		// TODO Auto-generated method stub
		return false;
	}

	public void render(Field field, Void constraintValue,
			MessageFormatter formatter, MarkupWriter writer,
			FormSupport formSupport) {
//		formSupport.addValidation(field, validationName, message, constraint).addClasspathScriptLink("/lichen/validator/Email.js");
//		formSupport.addScript(
//                "lichen.email('%s', %s);",
//                field.getClientId(),
//                quote(buildMessage(formatter, field)));
//		
	}

	
	
}
